//************************************************************************
//Developed by Guo Ming Li 2009/2010
//Source code:
//  The source code may be freely modified or used but this credit section may not be removed 
//  The original author must be notified of any modifications plus a copy of 
//  the changed source code
//Contact info: mailto://gming.li@gmail.com
//************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.RuleEngine;

namespace GML.BizTalk.OperationManager.Library
{
    public class RuleSetDeployment
    {
        public RuleSetDeployment() { }
        public static bool Undeploy(string RuleName, Nullable<int> MajorVersion, Nullable<int> MinorVersion)
        {
            return Exec("Undeploy", RuleName, MajorVersion, MinorVersion);
        }
        public static bool Deploy(string RuleName, Nullable<int> MajorVersion, Nullable<int> MinorVersion)
        {
            return Exec("Deploy", RuleName, MajorVersion, MinorVersion);
        }
        public static bool DeployPublished()
        {
            return Exec(null, null, null, null);
        }

        public static bool Exec(string action, string RuleName, Nullable<int> MajorVersion, Nullable<int> MinorVersion)
        {
            IRuleSetDeploymentDriver RSDeployDriver = null;
            SqlRuleStore SqlStore = null;
            RuleSetInfoCollection RSInfoCollection = null;
            try
            {
                RSDeployDriver = Configuration.GetDeploymentDriver();
                SqlStore = (SqlRuleStore)RSDeployDriver.GetRuleStore();
                RSInfoCollection = SqlStore.GetRuleSets(RuleStore.Filter.Published);

                if (string.IsNullOrEmpty(RuleName) || MajorVersion == null || MinorVersion == null)
                {
                    foreach (RuleSetInfo RSInfo in RSInfoCollection)
                    {
                        if (RSInfo.Published)
                        {
                            System.Diagnostics.Debug.WriteLine("Deploying " + RSInfo.Description + " " + RSInfo.Name + " " + RSInfo.MajorRevision + "." + RSInfo.MinorRevision);
                            Logger.doLog(Guid.NewGuid().ToString(), "Deploying " + RSInfo.Description + " " + RSInfo.Name + " " + RSInfo.MajorRevision + "." + RSInfo.MinorRevision, Logger.ServerityLevel.INFO);
                            RSDeployDriver.Deploy(RSInfo);
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("Deployed " + RSInfo.Description + " " + RSInfo.Name + " " + RSInfo.MajorRevision + "." + RSInfo.MinorRevision);
                            Logger.doLog(Guid.NewGuid().ToString(), "Deployed " + RSInfo.Description + " " + RSInfo.Name + " " + RSInfo.MajorRevision + "." + RSInfo.MinorRevision, Logger.ServerityLevel.INFO);
                        }
                    }
                }
                else
                {
                    foreach (RuleSetInfo RSInfo in RSInfoCollection)
                    {
                        System.Diagnostics.Debug.WriteLine("Found RSInfo = " + RSInfo.Description + " " + RSInfo.Name + " " + RSInfo.MajorRevision + "." + RSInfo.MinorRevision);
                        Logger.doLog(Guid.NewGuid().ToString(), "Found RSInfo = " + RSInfo.Description + " " + RSInfo.Name + " " + RSInfo.MajorRevision + "." + RSInfo.MinorRevision, Logger.ServerityLevel.INFO);
                        if (RSInfo.Name.Equals(RuleName) && RSInfo.MajorRevision.Equals(MajorVersion.Value) && RSInfo.MinorRevision.Equals(MinorVersion.Value))
                        {
                            if (action.Equals("Deploy"))
                            {
                                System.Diagnostics.Debug.WriteLine("Deploying " + RSInfo.Description + " " + RSInfo.Name + " " + RSInfo.MajorRevision + "." + RSInfo.MinorRevision);
                                Logger.doLog(Guid.NewGuid().ToString(), "Deploying " + RSInfo.Description + " " + RSInfo.Name + " " + RSInfo.MajorRevision + "." + RSInfo.MinorRevision, Logger.ServerityLevel.INFO);
                                RSDeployDriver.Deploy(RSInfo);
                            }
                            else if (action.Equals("Undeploy"))
                            {
                                System.Diagnostics.Debug.WriteLine("Undeploying " + RSInfo.Description + " " + RSInfo.Name + " " + RSInfo.MajorRevision + "." + RSInfo.MinorRevision);
                                Logger.doLog(Guid.NewGuid().ToString(), "Undeploying " + RSInfo.Description + " " + RSInfo.Name + " " + RSInfo.MajorRevision + "." + RSInfo.MinorRevision, Logger.ServerityLevel.INFO);
                                RSDeployDriver.Undeploy(RSInfo);
                            }
                        }
                    }

                }
                return true;

            }
            catch (Exception ex)
            {
                Logger.doLog(Guid.NewGuid().ToString(), ex.Message, Logger.ServerityLevel.ERROR);
                Logger.doLog(Guid.NewGuid().ToString(), ex.StackTrace, Logger.ServerityLevel.ERROR);
                throw ex;
            }
            finally
            {
                RSDeployDriver = null;
                SqlStore = null;
                RSInfoCollection = null;

            }
        }
    }
}
